import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/controller/reports.dart';
import 'package:ichinesereader/models/student/assignmentSearchDto.dart';
import 'package:ichinesereader/widgets/charts/classlevel.dart';
import 'package:ichinesereader/widgets/charts/classskill.dart';
import 'package:ichinesereader/widgets/grids/homeworkOngoingDatagrid.dart';
import '../../../themes/pageStyles.dart';

class ClassReportBottom extends StatefulWidget {
  @override
  State<ClassReportBottom> createState() => _ClassReportBottomState();
}

class _ClassReportBottomState extends State<ClassReportBottom> {
  final ReportsController _reportsController = Get.find();
  final box = Hive.box('appconfig');
  final PageStyles _pageStyles = PageStyles();

  @override
  void initState() {
    var classId = box.get(CacheManagerKey['currentclass'].toString());
    AssignmentSearchDto dto = new AssignmentSearchDto(
        assignmentType: "ASSIGNMENT",
        live: true,
        past: false,
        sortField: "-created");
    Future.delayed(Duration(microseconds: 100), (() {
      _reportsController.fetchClassAssignmentsData(classId, dto);
    }));
    super.initState();
  }

  Widget build(BuildContext context) {
    bool isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Teacher;
    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;

    return isSmallScreen
        ? Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                margin: EdgeInsets.symmetric(
                  vertical: 9.0,
                ),
                decoration: _pageStyles.dataGridBackground(),
                child: Container(
                  margin: EdgeInsets.all(
                    12.0,
                  ),
                  decoration: _pageStyles.dataGridForground(),
                  child: Obx(
                    () {
                      if (_reportsController.classleveldata.isNotEmpty)
                        return ClassLevelWidget(
                            levelchartdata: _reportsController.classleveldata);
                      else
                        return ClassLevelWidget(levelchartdata: []);
                      // return Text(
                      //   'Data not available'.tr,
                      //   style: TextStyle(
                      //       color:
                      //           Get.isDarkMode ? Colors.white : Colors.black),
                      // );
                    },
                  ),
                ),
              ),
              Container(
                margin: EdgeInsets.symmetric(vertical: 9.0),
                decoration: _pageStyles.dataGridBackground(),
                child: Obx(() {
                  if (_reportsController.classskilldata.isNotEmpty)
                    return AnimationClassSkillWidget(
                      skillchartData: _reportsController.classskilldata,
                    );
                  else
                    return AnimationClassSkillWidget(
                      skillchartData: [],
                    );
                  // return Text(
                  //   'Data not available'.tr,
                  //   style: TextStyle(
                  //       color: Get.isDarkMode ? Colors.white : Colors.black),
                  // );
                }),
              ),
              Obx(
                () {
                  if (_reportsController.classassignmentsresult.value.list !=
                          null &&
                      _reportsController
                          .classassignmentsresult.value.list!.isNotEmpty) {
                    var homeworks = _reportsController
                        .classassignmentsresult.value.list!
                        .where((element) => element.status == 'Live')
                        .toList();

                    return Container(
                        margin: EdgeInsets.only(top: 12.0),
                        decoration: BoxDecoration(),
                        child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              Padding(
                                padding: const EdgeInsets.only(bottom: 15.0),
                                child: Text(
                                  "Current Homework".tr,
                                  style: TextStyle(
                                      fontSize: 18.0,
                                      color: Get.isDarkMode
                                          ? Colors.white
                                          : Colors.black),
                                  overflow: TextOverflow.fade,
                                ),
                              ),
                              HomeworkOngoingDataGrid(isTeacher, homeworks
                                  // _reportsController
                                  //     .classassignmentsresult.value.list!
                                  )
                            ]));
                  } else {
                    return Container(
                      margin: EdgeInsets.only(top: 30.0),
                      child: Text(
                        "Currently no open homework".tr,
                        style: TextStyle(
                            color:
                                Get.isDarkMode ? Colors.white : Colors.black),
                      ),
                    );
                  }
                },
              ),
            ],
          )
        : Column(
            children: [
              Row(
                children: [
                  Flexible(
                    child: Container(
                      margin: EdgeInsets.only(
                        right: 6.0,
                      ),
                      decoration: _pageStyles.dataGridBackground(),
                      child: Container(
                        margin: EdgeInsets.all(12.0),
                        decoration: _pageStyles.dataGridForground(),
                        child: Obx(
                          () {
                            if (_reportsController.classleveldata.isNotEmpty)
                              return ClassLevelWidget(
                                  levelchartdata:
                                      _reportsController.classleveldata);
                            else
                              return ClassLevelWidget(levelchartdata: []);
                          },
                        ),
                      ),
                    ),
                  ),
                  Flexible(
                      child: Container(
                    margin: EdgeInsets.only(left: 4.5),
                    decoration: BoxDecoration(
                        color: Get.isDarkMode
                            ? Colors.black
                            : Colors.white.withOpacity(0.3),
                        borderRadius: BorderRadius.circular(3.0)),
                    child: Obx(() {
                      if (_reportsController.classskilldata.isNotEmpty)
                        return AnimationClassSkillWidget(
                          skillchartData: _reportsController.classskilldata,
                        );
                      else
                        return AnimationClassSkillWidget(
                          skillchartData: [],
                        );
                    }),
                  )),
                ],
              ),
              Obx(
                () {
                  if (_reportsController.classassignmentsresult.value.list !=
                          null &&
                      _reportsController
                          .classassignmentsresult.value.list!.isNotEmpty) {
                    var homeworks = _reportsController
                        .classassignmentsresult.value.list!
                        .where((element) => element.status == 'Live')
                        .toList();

                    return Container(
                        margin: EdgeInsets.only(top: 30.0),
                        decoration: BoxDecoration(),
                        child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              Padding(
                                padding: const EdgeInsets.only(bottom: 12.0),
                                child: Text(
                                  "Current Homework".tr,
                                  style: TextStyle(
                                      fontSize: 18.0,
                                      color: Get.isDarkMode
                                          ? Get.theme.primaryColorDark
                                          : Colors.black),
                                  overflow: TextOverflow.fade,
                                ),
                              ),
                              HomeworkOngoingDataGrid(isTeacher, homeworks
                                  // _reportsController
                                  //     .classassignmentsresult.value.list!
                                  )
                            ]));
                  } else {
                    return Container(
                      margin: EdgeInsets.only(top: 30.0),
                      child: Text(
                        "Currently no open homework".tr,
                        style: TextStyle(
                            fontSize: 18.0,
                            color: Get.isDarkMode
                                ? Get.theme.primaryColorDark
                                : Colors.black),
                      ),
                    );
                  }
                },
              ),
            ],
          );
  }
}
